# 19. 删除链表的倒数第 N 个结点
# 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
# 输入：head = [1,2,3,4,5], n = 2
# 输出：[1,2,3,5]

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def removeNthFromEnd(self, head:[ListNode], n: int) ->[ListNode]:
        dummy = ListNode()
        dummy.next = head
        slow, fast = dummy, dummy
        n = n + 1
        while n and fast:
            fast = fast.next
            n -= 1
        while fast:
            slow = slow.next
            fast = fast.next
        slow.next = slow.next.next
        return dummy.next

if __name__ == '__main__':
    a = ListNode(1)
    b = ListNode(2)
    c = ListNode(3)
    d = ListNode(4)
    e = ListNode(3)
    a.next = b
    b.next = c
    c.next = d
    d.next = e
    ss = Solution()
    rr = ss.removeNthFromEnd(a,2)
    while rr != None:
        print(rr.val)
        rr = rr.next
#怎么写都不对，不如抹掉全部重写，欸，这不就对了
